<?php

/**
 * Returns module name
 *
 * @return string
 */
function current_module() {
    $ci = get_instance();
    $routes = load_class('Router');
    return $routes->fetch_module();
}

/**
 * Returns current module (object) not just the name
 *
 * @return object
 */
function current_module_obj() {
    return Module_Extended::get_module_obj(current_module());
}

/**
 * Returns module (object) by name
 * @param string $module_name
 * @return object
 */
function get_module_obj($module_name) {
    return Module_Extended::get_module_obj($module_name);
}

/**
 * Returns current controller  name
 * 
 * @return string
 */
function current_controller() {
    $ci = get_instance();
    $routes = load_class('Router');
    return $routes->fetch_class();
}

/**
 * Retuns current module path
 * i.e ./application/modules/$curent_module/$folder
 *
 * @param string $folder
 * @return string
 */
function current_module_path($folder='') {
    $module_name = current_module();
    return get_module_path($module_name, $folder);
}

/**
 * Retuns module path
 * i.e ./application/modules/$module_name/$folder
 *
 * @param string $module_name
 * @param string $folder
 * @return string
 */
function get_module_path($module_name, $folder='') {
    if (strlen($folder))
        return APPPATH . "modules/$module_name/$folder/";
    else
        return APPPATH . "modules/$module_name/";
}

/**
 * get id of a datamapper object
 *
 * @param int|object $object_or_id
 * @param string $id_field_name
 * @return int
 */
function get_id($object_or_id, $id_field_name='id') {
    if (is_object($object_or_id)) {
        return $object_or_id->{$id_field_name};
    } else {
        return $object_or_id;
    }
}

/**
 * Returns datamapper object is the supplied $object_or_id is already not an object
 * then a object of $class is created and returned
 *
 * @param int|object $object_or_id
 * @param string $class name of the class
 * @return NULL|object object of the specified class
 */
function get_object($object_or_id, $class) {
    if (is_object($object_or_id) && is_a($object_or_id, $class)) {
        return $object_or_id;
    } else {
        $obj = new $class();
        $obj->get_by_id($object_or_id);
        if ($obj->exists()) {
            return $obj;
        } else {
            return NULL;
        }
    }
}

/**
 * Curent language of the site
 * 
 * @return  language_m_core
 */
function get_current_language() {
    return service_l_core::get_current_language();
}

function batch_simple_data($arr_a_base_models) {
    if (count($arr_a_base_models) > 0) {
        $simple_data = array();
        foreach ($arr_a_base_models as $model) {
            $simple_data[] = $model->convert_to_simple_data();
        }
        return $simple_data;
    } else {
        return $arr_a_base_models;
    }
}

/**
 * Include a file inside a module in a specific folder quickly
 *
 * @param string $module
 * @param string $class_name
 * @param string $folder
 * @return TRUE|NULL TRUE is file is successfully included
 */
function quick_include($module, $class_name, $folder="drivers") {
    $path = get_module_path($module, $folder);
    $driver = $path . "$class_name" . EXT;
    if (file_exists($driver)) {
        include_once $driver;
        return TRUE;
    } else {
        return NULL;
    }
}

/**
 * Using codeigniter's paging library and given params this function returns
 * pagination links.
 * example :
 * $url_trail = "/p~9:asc";
 * $this->page_link = get_pagigination("search/by/part_num_desc/", $this->count,  module_u_search::DEFAULT_PER_PAGE,$url_tail);
 *
 * @param string $base_url_part add these parts after the base_url()/index.php/ portion
 * @param int $total_rows total num of items
 * @param int $per_page num of items per page
 * @param string $url_aditional_data additional segments 
 * @param string $uri_segment
 * @return string page link
 */
function get_pagigination($base_url_part, $total_rows, $per_page, $url_aditional_data="", $uri_segment="") {

    get_instance()->load->library('pagination');
    $config['base_url'] = base_url() . "index.php/" . $base_url_part;
    $config['total_rows'] = $total_rows;
    $config['per_page'] = $per_page;
    if (strlen($url_aditional_data))
        $config['url_adiditions'] = "/" . $url_aditional_data;
    if (strlen($uri_segment))
        $config['uri_segment'] = $uri_segment;
    get_instance()->pagination->initialize($config);
    return get_instance()->pagination->create_links();
}

/**
 * This function is a shorthand for the $this->input->post('name') function
 * In additional it can take a default value if the $_POST field is missing
 * in such case the default value is returned
 * i.e. post('username','guest',TRUE)
 *
 * @param string $name
 * @param string $default
 * @param boolean $xss_clean
 * @return string|FALSE
 */
function post($name,$default=FALSE,$xss_clean=FALSE){
    $value = get_instance()->input->post($name);
    if($value===FALSE) $value=$default;
    return $value===FALSE ? $value : ($xss_clean===TRUE ? xss_clean($value) : $value) ;
}
?>